package org;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.ImageIcon;

@SuppressWarnings("serial")
public class MyWindow extends JFrame implements ChangeListener {
	volatile static private int counter;
	private JPanel contentPane;
	private JTextField red = new JTextField();
	private JSlider slider1 = new JSlider();
	private JPanel rightPanel = new JPanel();
	private JPanel leftPanel = new JPanel();
	private JLabel lblGreen = new JLabel("Green");
	private JTextField green = new JTextField();
	private JSlider slider2 = new JSlider();
	private JLabel lblBlue = new JLabel("Blue");
	private JTextField blue = new JTextField();
	private JSlider slider3 = new JSlider();
	private JButton btnSair = new JButton("Sair");
	private JButton btnSobre = new JButton("Sobre");

	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MyWindow frame = new MyWindow();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public MyWindow() {
		blue.setName("blue");
		blue.setColumns(3);
		blue.setEditable(false);
		
		green.setName("green");
		green.setColumns(3);
		green.setEditable(false);
		
		setBackground(Color.WHITE);
		setResizable(false);
		setTitle("RGB Color Model");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 400, 200);
		contentPane = new JPanel();
		contentPane.setBackground(Color.WHITE);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new GridLayout(0, 2, 0, 0));
		
		leftPanel.setBackground(Color.WHITE);
		contentPane.add(leftPanel);
		
		JLabel lblRed = new JLabel("Red");
		lblRed.setMinimumSize(new Dimension(30, 15));
		lblRed.setMaximumSize(new Dimension(30, 15));
		lblRed.setHorizontalAlignment(SwingConstants.RIGHT);
		lblRed.setHorizontalTextPosition(SwingConstants.LEADING);
		lblRed.setPreferredSize(new Dimension(30, 15));
		lblRed.setName("lblRed");
		leftPanel.add(lblRed);
		
		red.setEditable(false);
		red.setName("red");
		leftPanel.add(red);
		red.setColumns(3);
		slider1.setForeground(Color.BLACK);
		
		slider1.setBackground(Color.WHITE);
		slider1.setMaximum(255);
		slider1.setName("slider1");
		slider1.setFocusTraversalPolicyProvider(true);
		slider1.addChangeListener(this);
		slider1.setPreferredSize(new Dimension(100, 23));
		slider1.setValue(0);
		leftPanel.add(slider1);
		lblGreen.setName("lblGreen");
		lblGreen.setHorizontalTextPosition(SwingConstants.LEADING);
		lblGreen.setHorizontalAlignment(SwingConstants.RIGHT);
		
		leftPanel.add(lblGreen);
		
		leftPanel.add(green);
		slider2.setName("slider2");
		slider2.setPreferredSize(new Dimension(100, 23));
		slider2.setForeground(Color.BLACK);
		slider2.setBackground(Color.WHITE);
		slider2.setMaximum(255);
		slider2.addChangeListener(this);
		slider2.setValue(0);
		leftPanel.add(slider2);
		
		lblBlue.setName("lblBlue");
		lblBlue.setMinimumSize(new Dimension(30, 15));
		lblBlue.setMaximumSize(new Dimension(30, 15));
		lblBlue.setHorizontalTextPosition(SwingConstants.LEADING);
		lblBlue.setHorizontalAlignment(SwingConstants.RIGHT);
		lblBlue.setPreferredSize(new Dimension(30, 15));
		leftPanel.add(lblBlue);
		
		leftPanel.add(blue);
		slider3.addChangeListener(this);
		
		slider3.setName("slider3");
		slider3.setMaximum(255);
		slider3.setValue(0);
		slider3.setBackground(Color.WHITE);
		slider3.setForeground(Color.BLACK);
		slider3.setPreferredSize(new Dimension(100, 23));
		leftPanel.add(slider3);
		
		btnSair.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				System.exit(0);
			}
		});
		
		leftPanel.add(btnSair);
		btnSobre.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				Sobre sobre = new Sobre();
				sobre.setVisible(true);
			}
		});
		leftPanel.add(btnSobre);
		
		rightPanel.setBackground(Color.BLACK);
		contentPane.add(rightPanel);
	}

	public JTextField getRed() {
		return red;
	}
	public JSlider getSlider1() {
		return slider1;
	}

	@Override
	public void stateChanged(ChangeEvent event) {
		int red=0;
		int green=0;
		int blue=0;
		if (counter < 2) counter++;
		else {
			red = getSlider1().getValue();
			green = getSlider2().getValue();
			blue = getSlider3().getValue();
		}
		
		getRed().setText( new Integer(red).toString() );
		getGreen().setText( new Integer(green).toString() );
		getBlue().setText( new Integer(blue).toString() );
		
		getRightPanel().setBackground(new Color(red, green, blue));
	}
	public JPanel getRightPanel() {
		return rightPanel;
	}
	public JPanel getLeftPanel() {
		return leftPanel;
	}
	public JSlider getSlider2() {
		return slider2;
	}
	public JSlider getSlider3() {
		return slider3;
	}
	public JTextField getGreen() {
		return green;
	}
	public JTextField getBlue() {
		return blue;
	}
}
